package com.etang.a2020a1018.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.bizideal.smarthome.socket.LoginCallback;
import com.bizideal.smarthome.socket.SocketClient;
import com.etang.a2020a1018.R;
import com.etang.a2020a1018.tools.AppConfig;
import com.etang.a2020a1018.tools.DiyToast;

import java.util.ArrayList;
import java.util.List;


public class BarActivity extends FragmentActivity {

    ViewPager pager;
    List<Fragment> list_fargment = new ArrayList<Fragment>();
    Fragment_check fragment_check;
    RadioButton ra_base, ra_control, ra_value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        ra_base = (RadioButton) findViewById(R.id.ra_base);
        ra_control = (RadioButton) findViewById(R.id.ra_control);
        ra_value = (RadioButton) findViewById(R.id.ra_value);
        list_fargment.add(new BaseActivity());
        list_fargment.add(new ControlActivity());
        list_fargment.add(new ValueActivity());
        pager = (ViewPager) findViewById(R.id.pager);
        fragment_check = new Fragment_check(getSupportFragmentManager());
        pager.setAdapter(fragment_check);
        pager.setOffscreenPageLimit(list_fargment.size());
        ra_value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(2);
            }
        });
        ra_control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(1);
            }
        });
        ra_base.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(0);
            }
        });
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("TAG", "onPageScrolled: " + position);
                switch (position) {
                    case 0:
                        ra_base.setChecked(true);
                        ra_control.setChecked(false);
                        ra_value.setChecked(false);
                        break;
                    case 1:
                        ra_control.setChecked(true);
                        ra_base.setChecked(false);
                        ra_value.setChecked(false);
                        break;
                    case 2:
                        ra_value.setChecked(true);
                        ra_control.setChecked(false);
                        ra_base.setChecked(false);
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        SharedPreferences sharedPreferences = getSharedPreferences("rember", MODE_PRIVATE);
        ControlUtils.setUser("bizideal", "123456", sharedPreferences.getString("ip", null));
        SocketClient.getInstance().creatConnect();
        SocketClient.getInstance().login(new LoginCallback() {
            @Override
            public void onEvent(final String s) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (s.equalsIgnoreCase(ConstantUtil.SUCCESS)) {
                            DiyToast.showToast(getApplicationContext(), "组网成功");
                            AppConfig.getdata(BarActivity.this);
                        } else if (s.equalsIgnoreCase(ConstantUtil.FAILURE)) {
                            DiyToast.showToast(getApplicationContext(), "组网失败");
                            AppConfig.getdata(BarActivity.this);
                        } else {
                            DiyToast.showToast(getApplicationContext(), "组网异常");
                            AppConfig.getdata(BarActivity.this);
                        }
                    }
                });
            }
        });
    }

    public class Fragment_check extends FragmentPagerAdapter {

        public Fragment_check(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public androidx.fragment.app.Fragment getItem(int position) {
            return list_fargment.get(position);
        }

        @Override
        public int getCount() {
            return list_fargment.size();
        }
    }

}